﻿using System;   
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LarasMutiaraDiva.SkripsiLaras.Lib;

namespace LarasMutiaraDiva.SkripsiLaras.UI.Web.Controllers
{
    public class SearchController : Controller
    {
        //
        // GET: /Search/


        public ActionResult Index()
        {
            return View();
        }

        public void AjaxSearch()
        {
            var query = Request.QueryString["userInput"];
            var enableEkspansi = Request.QueryString["enableEkspansi"];

            Program.NUMBER_OF_TOP_CONCEPTS = 1;
            Program.NUMBER_OF_TOP_PASSAGES = 1;
            Program.NUMBER_OF_TOP_RELEVAN_DOCS= 1;

            //Response.Write(query);
            //Program lib=new Program();
            var START = DateTime.Now;
            var processQuery = Program.GetStemQuery(query);         //stemmed user query

            var docEkspansi = new List<string>();
            var queryEkspansi = string.Empty;
            //Response.Write(stemmedQuery);
            if (enableEkspansi == "on")
            {
                queryEkspansi = Program.GetNewQuery(processQuery);       //get new expansion query from stemmedQuery
                docEkspansi = Program.GetTopNDocByQuery(queryEkspansi, int.MaxValue);
            }
            
            //var docs = Program.GetTopNDocByQuery(stemmedQuery , int.MaxValue);  //top n doc without exspansion
            var docs = Program.GetTopNDocByQuery(processQuery, int.MaxValue);        //top n doc with exspansion
            docs = docEkspansi.MergeList(docs,int.MaxValue);

            var END = DateTime.Now;
            int time=(END-START).Milliseconds;
            Response.Write(string.Format("<span style='float: right; font-size: 11px;'>{0} Documents in {1} seconds</span><br />", docs.Count,(float) time/1000 ));
            Response.Write("New Query: "+queryEkspansi+" || "+processQuery+"<br />");
            string htmlText = string.Empty;
            foreach (var doc in docs)
            {
                htmlText += "<div class='line_search'>";
                var docName = doc.TrimStart().TrimEnd().Replace(' ', '-') + ".txt";
                var previewText = Program.GetDocPreview(docName,query, 50);
                var docAddress = @"../../documents/"+docName;
                htmlText+= string.Format("<a href='{0}' target='blank'>{1}</a> &nbsp;<button doc='{2}' class='buttonQuickPreview' />", docAddress, docName, docName);
                htmlText += string.Format("<br /><span class='preview'>{0}</span><br /> <br />", previewText);
                htmlText += "</div>";
            }
            Response.Write(htmlText);
             
        }

        

        public void QuickPreview()
        {
            var docName = Request.Form["docName"];
            var query = Request.Form["query"];
            var docContent = Program.GetContentDoc(docName);
            var listQuery = Program.GetWords(query);
            foreach (var term in listQuery)
            {
                docContent = docContent.Replace(term, "<b>" + term + "</b>");
            }
            //docContent= "<input type='checkbox' onclick=\"DisplayAsXml()\" />Display as XML<hr style='border-style:dotted;' />" + docContent;
            Response.Write(docContent);

        }
    }
    public static class Extension
    {
        //ekspansi merge non ekspansi
        public static List<string> MergeList(this List<string> listExspansion, List<string> listNoExspansion, int numberDocsMerge)
        {

            for (int i=0; i<listNoExspansion.Count() && listExspansion.Count() < numberDocsMerge;i++)
            {
                var kata2 = listNoExspansion[i];

                if (!listExspansion.Contains(kata2))
                {
                    listExspansion.Add(kata2);
                }
            }
            var retValue = listExspansion;
            if(listExspansion.Count > numberDocsMerge)
            {
                retValue = listExspansion.Take(numberDocsMerge).ToList();
            }
            return retValue;
        }

        // non ekspansi merge ekspansi
        public static List<string> MergeList2(this List<string> listExspansion, List<string> listNoExspansion, int numberDocsMerge)
        {

            for (int i = 0; i < listExspansion.Count() && listNoExspansion.Count() < numberDocsMerge; i++)
            {
                var kata2 = listExspansion[i];

                if (!listNoExspansion.Contains(kata2))
                {
                    listNoExspansion.Add(kata2);
                }
            }
            var retValue = listNoExspansion;
            if (listNoExspansion.Count > numberDocsMerge)
            {
                retValue = listNoExspansion.Take(numberDocsMerge).ToList();
            }
            return retValue;
        }
    }
}
